// pages/Details/Details.js
import {request} from "../../../request/index";
import {sendChat} from "../../../request/index";
var util = require('../../../utils/util.js'); //参数是util.js所在的路径，参照自个儿的

Page({
  click:function(event){
    var index=event.currentTarget.dataset.key;
    // console.log(index)
     wx.navigateTo({
       url: '/pages/PersonalDetails/PersonalDetails?index='+index
     })
  },
  /**
   * 页面的初始数据
   */
  data: {
    historyMsgList:[],
    friendInfo:[],
    userInfo:[],
    userId:null,
    friendId:null
  },
  QueryParams:{
    pagenum:1,
    pagesize:5
  },
  totalpages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
   
    this.setData({
      userInfo:wx.getStorageSync("userInfo"),
      userId:wx.getStorageSync("userInfo").userId.toString()
    })
    
  this.GetDetailChat();
     
  },
async GetDetailChat(){
  const eventChannel =this.getOpenerEventChannel();
      await eventChannel.on(
        'friendInfo', 
         data=> {
          this.setData({
            friendInfo:data.friendInfo,
            friendId:data.friendInfo.userId.toString(),
          })
      }
    )
    var friendName = this.data.friendInfo.nickName;
    wx.setNavigationBarTitle({
      title: friendName,
    })
    this.getChatMsg();
},
  async getChatMsg(){
    const sender=wx.getStorageSync("userInfo").userId.toString();
    var pageSize = this.QueryParams.pagesize;
    var pageNum = this.QueryParams.pagenum;
    var reciver = this.data.friendId;
    const res=await request({
      url:"http://localhost:8080/ipark/chat/getdchat",
      data:{
        pageSize,pageNum,reciver,sender
      }
    });
    this.totalPages = res.data.message;
    console.log(res)
    this.setData({
      historyMsgList:[...res.data.data.HistoryMsg.reverse(),...this.data.historyMsgList]
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
// 点击键盘上的搜索取值
  bindconfirm(e){
  let content = e.detail.value['search - input'] ?e.detail.value['search - input'] : e.detail.value;
  let reciver = this.data.friendId;
  let sender = this.data.userId
  console.log('e.detail.value', content )
  var that = this;
  //发送消息
   wx.sendSocketMessage({
    data:reciver+"#"+content,
    success: function (resSend) {
      console.log("resSend:")
      console.log(resSend)
       const sendchat =  sendChat({
        url:"http://localhost:8080/ipark/chat/savechat",
        data:{sender:sender,reciver:reciver,msg:content,senddate:null},
      });
      sendchat.then(data=>{
        that.setData({
        historyMsgList:[...that.data.historyMsgList,...data.data.data.sendChat]
      })   
      })
     
    },
    fail: function (resSendError) {
      console.log("resSendError: ")
      console.log(resSendError)
    }
  })
  wx.onSocketMessage(function (resMessage) {//接收返回消息
    console.log("resMessage: ")
    console.log(resMessage);
  })
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.QueryParams.pagenum>=this.totalPages){
      wx.showToast({
        title: '已经没有了',
      });
    }else{
      this.QueryParams.pagenum++;
      this.getChatMsg();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})